package cn.onepiece.smili.render.handler;

import cn.onepiece.smili.render.info.TableInfo;
import cn.onepiece.smili.util.MysqlToJavaTypeMapping;
import com.google.common.base.CaseFormat;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;

/**
 * @Author: haoting.wang
 * @Date: Created in 下午6:04 2017/5/27
 */
public class TableHandler {

    private static Logger logger = LoggerFactory.getLogger(TableHandler.class);
    /**
     * 解析列信息
     *
     * @param table ResultSet
     * @return 列信息
     * @throws SQLException
     */
    public static TableInfo.ColumnInfo parseColumnInfo(ResultSet table) throws SQLException {
        TableInfo.ColumnInfo columnInfo = new TableInfo.ColumnInfo();

        String columnName = table.getString("COLUMN_NAME");

        columnInfo.initColumnName(columnName);


        String typeName = table.getString("TYPE_NAME");
        String jdbcType = MysqlToJavaTypeMapping.getJdbcType(typeName);
        String javaType = MysqlToJavaTypeMapping.getJavaType(typeName);

        columnInfo.setJdbcType(jdbcType);
        columnInfo.setJavaType(javaType);

        String remarks = table.getString("REMARKS");
        if(!StringUtils.isBlank(remarks)){
            columnInfo.setRemark(table.getString("remarks"));
        }
        return columnInfo;
    }

    public static DatabaseMetaData getDatabaseMetaData(Connection connection) {
        try {
            return connection.getMetaData();
        } catch (SQLException e) {
            logger.error("Connection getMetaData error,e={}", e.getMessage());
            return null;
        }
    }



    public static TableInfo parseResultSet(DatabaseMetaData metaData,String tName){
        ResultSet table = null;
        ResultSet pk = null;
        try {
            table = metaData.getColumns(null, "%", tName, "%");
            pk = metaData.getPrimaryKeys(null, null, tName);
        } catch (SQLException e) {
            logger.error("DatabaseMetaData getColumns error,e={}", e.getMessage());
        }

        TableInfo tableInfo = new TableInfo();
        tableInfo.initClassName(tName);

        Set<String> importList = tableInfo.getImportList();
        List<TableInfo.ColumnInfo> columnInfos = tableInfo.getColumnInfoList();

        try {
            //寻找主键，支持单一主键
            while (pk.next()){
                tableInfo.initPkName(pk.getString("COLUMN_NAME"));

                // 获取不到。。。。
                // tableInfo.setPkType(pk.getString("TYPE_NAME"));
            }

            //解析列
            while (table.next()) {
                TableInfo.ColumnInfo columnInfo = parseColumnInfo(table);

                if(columnInfo.getLowerCamelName().equalsIgnoreCase(tableInfo.getPkName())){
                    tableInfo.setPkType(columnInfo.getJavaType());
                }

                if (columnInfo.getJavaType().equals("BigDecimal")){
                    importList.add("java.math.BigDecimal");
                }else if(columnInfo.getJavaType().equals("Date")){
                    importList.add("java.util.Date");
                }

                columnInfos.add(columnInfo);
            }
        } catch (SQLException e) {
            logger.error("ResultSet getString error,e={}", e.getMessage());
        }

        return tableInfo;
    }
}
